package cash.cmp.personal_financial_system.bigdata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.sql.*;
import java.util.*;

/**
 * @Description:
 * @author: luoy
 * @date: 2020-06-24 09:45
 */
@Configuration
public class PrestoConnect {


    @Value("${presto.url}")
    private String server;

    @Value("${presto.port}")
    private String port;

    @Value("${presto.username}")
    private String username;

    @Value("${presto.password}")
    private String password;

    private Connection getConnection() throws Exception {

        /**
         * 功能：presto 通过 jdbc 连接
         * 示例：jdbc:presto://host:port/
         **/
        String jdbcurl = "jdbc:presto://" + server + ":" + port + "/";
        Connection conn ;
        Properties props = new Properties();
        Class.forName("com.facebook.presto.jdbc.PrestoDriver");
        props.setProperty("user", username);
        if (password != null && !password.equals("")) {
            props.setProperty("password", password);
            props.setProperty("SSL", "true");
            //props.setProperty("SSLTrustStorePath", SSLTrustStorePath);
            //props.setProperty("SSLTrustStorePassword", SSLTrustStorePassword);
            jdbcurl = String.format("jdbc:presto://%s:%s/", server, port);
        }
        conn = DriverManager.getConnection(jdbcurl, props);

        /*设置连接的数据源类型
         * 示例:mysql、hive
         */
        conn.setCatalog("hive");
        return conn;
    }

    public List getDataAll(String sql)
            throws Exception {
        List<Map<String,String>> list = new ArrayList<>();
        Statement ps = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            con = getConnection();
            System.out.println(con);
//            if(con == null){
//                List<Integer> arr = new ArrayList<>();
//                arr.add(90);
//                return arr;
//            }
            ps = con.createStatement();
            rs = ps.executeQuery(sql);
            // 获取列数
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            // 遍历ResultSet中的每条数据
            while (rs.next()) {
                Map<String,String> map = new HashMap<>();
                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);
                    if(columnName == null || "".equals(columnName)){
                        map.put(columnName, "");
                    }
                    else {
                        String value = rs.getString(columnName);
                        map.put(columnName, value);
                    }

                }
                list.add(map);
            }
        } catch (Exception e) {
            throw new Exception("ERROR:" + e.getMessage(), e);
        } finally {
            //关闭资源(先开后关)
            if(rs != null) rs.close();
            if(ps != null) ps.close();
            if(con != null) con.close();

        }
        return list;
    }
}


